﻿using MediatR;
using SheepCultivation.Common.Domain;
using SheepCultivation.Common.Dto;
using SheepCultivation.Common.Infrastructure.Interface;
using SheepCultivation.Enum;

namespace SheepCultivation.API.AddliCation.EnterPriseW
{
    public class EnterPriseSeachHandler : IRequestHandler<EnterPriseSeachCommon, Result>
    {
        public readonly IBaseRepository<Enterprise> EnterpriseRepsoitory;
        public readonly IBaseRepository<Province> ProvinceRepsoitory;
        public readonly IBaseRepository<City> CityRepsoitory;
        public readonly IBaseRepository<County> CountyRepsoitory;
        public readonly IBaseRepository<Industry> IndustryRepsoitory;
        public readonly IBaseRepository<Accountingunit> AccountingunitRepsoitory;
        public readonly ILogger<EnterPriseSeachHandler> logger;

        public EnterPriseSeachHandler(IBaseRepository<Enterprise> enterpriseRepsoitory, IBaseRepository<Province> provinceRepsoitory, IBaseRepository<City> cityRepsoitory, IBaseRepository<County> countyRepsoitory, IBaseRepository<Industry> industryRepsoitory, IBaseRepository<Accountingunit> accountingunitRepsoitory, ILogger<EnterPriseSeachHandler> logger)
        {
            EnterpriseRepsoitory = enterpriseRepsoitory;
            ProvinceRepsoitory = provinceRepsoitory;
            CityRepsoitory = cityRepsoitory;
            CountyRepsoitory = countyRepsoitory;
            IndustryRepsoitory = industryRepsoitory;
            AccountingunitRepsoitory = accountingunitRepsoitory;
            this.logger = logger;
        }

        public async Task<Result> Handle(EnterPriseSeachCommon request, CancellationToken cancellationToken)
        {
            var res = new Result() { Code = (int)CommonEnumCode.error };
            try
            {
                var data = new PageDto() { };
                var list = (from a in EnterpriseRepsoitory.GetAll()
                            join b in ProvinceRepsoitory.GetAll()
                            on a.ProvinceId equals b.ProvinceId
                            join c in CityRepsoitory.GetAll()
                            on a.CityId equals c.CityId
                            join d in CountyRepsoitory.GetAll()
                            on a.CountyId equals d.CountyId
                            join e in IndustryRepsoitory.GetAll()
                            on a.IndustryId1 equals e.IndustryId
                            join f in AccountingunitRepsoitory.GetAll()
                            on a.AccountingunitId1 equals f.AccountingunitId
                            select new EnterDto()
                            {
                                EnterpriseId = a.EnterpriseId,
                                EnterpriseName = a.EnterpriseName,
                                Enterpriseillustrate = a.Enterpriseillustrate,
                                EnterpriseLogo = a.EnterpriseLogo,
                                IndustryId1 = a.IndustryId1,
                                IndustryName1 = IndustryRepsoitory.GetAll().Where(x => x.IndustryId == a.IndustryId1).FirstOrDefault().IndustryName,
                                IndustryId2 = a.IndustryId2,
                                IndustryName2 = IndustryRepsoitory.GetAll().Where(x => x.IndustryId == a.IndustryId2).FirstOrDefault().IndustryName,
                                ProvinceId = a.ProvinceId,
                                ProvinceName = b.ProvinceName,
                                CityId = a.CityId,
                                CityName = c.CityName,
                                CountyId = a.CountyId,
                                CountyName = d.CountyName,
                                FullAddress = a.FullAddress,
                                EnterprisePhone = a.EnterprisePhone,
                                Unifiedsocialcode = a.Unifiedsocialcode,
                                Description = a.Description,
                                Deactivated = false,
                                Head = a.Head,
                                ApartHead = a.ApartHead,
                                FinanceHead = a.FinanceHead,
                                MyProperty = a.MyProperty,
                                AccountingunitId1 = a.AccountingunitId1,
                                AccountingunitName1 = AccountingunitRepsoitory.GetAll().Where(x => x.AccountingunitId == a.AccountingunitId1).FirstOrDefault().AccountingunitName,
                                AccountingunitId2 = a.AccountingunitId2,
                                AccountingunitName2 = AccountingunitRepsoitory.GetAll().Where(x => x.AccountingunitId == a.AccountingunitId2).FirstOrDefault().AccountingunitName
                            }).ToList();
                data.PageCount = list.Count();
                data.TotalCount = (int)Math.Ceiling((list.Count() * 1.0) / request.pagesize);
               data.data= list.OrderBy(x => x.EnterpriseId).Skip((request.pageindex - 1) * request.pagesize).Take(request.pagesize).ToList();
                res.Code = (int)CommonEnumCode.success;
                res.Data = data;
                res.Msg = "列表展示";
            }
            catch (Exception)
            {

                throw;
            }

            return res;
        }
    }
    public class PageDto
    {
        public int PageCount { get; set; }
        public int TotalCount { get; set; }
        public List<EnterDto> data { get; set; }
    }
    public class EnterDto
    {
        public int EnterpriseId { get; set; }
        /// <summary>
        /// 企业名称
        /// </summary>
        public string EnterpriseName { get; set; }
        /// <summary>
        /// 企业简介
        /// </summary>
        public string Enterpriseillustrate { get; set; }
        /// <summary>
        /// 企业图片logo
        /// </summary>
        public string EnterpriseLogo { get; set; }
        /// <summary>
        /// 一级行业标签
        /// </summary>
        public int IndustryId1 { get; set; }
        /// <summary>
        /// 二级行业标签
        /// </summary>
        public int IndustryId2 { get; set; }
        /// <summary>
        /// 所在区域
        /// </summary>
        public int ProvinceId { get; set; }
        public int CityId { get; set; }
        public int CountyId { get; set; }
        /// <summary>
        /// 详细地址
        /// </summary>
        public string FullAddress { get; set; }
        /// <summary>
        /// 企业电话
        /// </summary>
        public string EnterprisePhone { get; set; }
        /// <summary>
        /// 同一社会信用代码
        /// </summary>
        public string Unifiedsocialcode { get; set; }
        /// <summary>
        /// 描述
        /// </summary>
        public string Description { get; set; }
        /// <summary>
        /// 停用企业
        /// </summary>
        public bool Deactivated { get; set; }
        /// <summary>
        /// 负责人
        /// </summary>
        public string Head { get; set; }
        /// <summary>
        /// 财务负责人
        /// </summary>
        public string FinanceHead { get; set; }
        /// <summary>
        /// 分管负责人
        /// </summary>
        public string ApartHead { get; set; }
        /// <summary>
        /// 可查看人员
        /// </summary>
        public int MyProperty { get; set; }
        /// <summary>
        /// 核算单元
        /// </summary>
        public int AccountingunitId1 { get; set; }
        public int AccountingunitId2 { get; set; }
        public string ProvinceName { get; set; }
        public string CityName { get; set; }
        public string CountyName { get; set; }
        public string AccountingunitName1 { get; set; }
        public string AccountingunitName2 { get; set; }
        public string IndustryName1 { get; set; }
        public string IndustryName2 { get; set; }
    }

}
